package com.zj.mvplce.utils.permisson

import android.Manifest
import android.os.Build
import android.util.Log
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ToastUtils
import com.example.location_library.R

/**
 * 版权：渤海新能 版权所有
 * @author zhujiang
 * 版本：1.5
 * 创建日期：2020/4/8
 * 描述：权限申请工具类
 *
 */
object PermissionXUtils {

    fun getPermission(
        activity: FragmentActivity,
        callback: PermissionXUtilsCallback,
        vararg permissions: String
    ) {
        if (Build.VERSION.SDK_INT >= 23) {
            PermissionX.init(activity)
                .permissions(
                    *permissions
                )
                .onExplainRequestReason { deniedList ->
                    showRequestReasonDialog(
                        deniedList,
                        "${activity.resources.getString(R.string.activity_permission)}\n\n${getDeniedList(
                            deniedList
                        )}",
                        activity.resources.getString(R.string.dialog_content_circle_text_4),
                        activity.resources.getString(R.string.dialog_content_circle_text_3)
                    )
                }
                .onForwardToSettings { deniedList ->
                    showForwardToSettingsDialog(
                        deniedList,
                        "${activity.resources.getString(R.string.activity_permission_setting)}\n\n${getDeniedList(
                            deniedList
                        )}",
                        activity.resources.getString(R.string.dialog_content_circle_text_4),
                        activity.resources.getString(R.string.dialog_content_circle_text_3)
                    )
                }
                .request { allGranted, grantedList, deniedList ->
                    if (allGranted) {
                        callback.permissionCallback()
                    } else {
                        ToastUtils.showLong("已拒绝：${getDeniedList(deniedList)}")
                        Log.e("拒绝权限: ", "拒绝 $deniedList")
                    }
                }
        } else {
            callback.permissionCallback()
        }
    }

    private fun getDeniedList(deniedList: List<String>): String {
        var index = 1
        val result = StringBuilder()
        /**
         * group:android.permission-group.CONTACTS
        2   permission:android.permission.WRITE_CONTACTS
        3   permission:android.permission.GET_ACCOUNTS
        4   permission:android.permission.READ_CONTACTS
        5
        6 group:android.permission-group.PHONE
        7   permission:android.permission.READ_CALL_LOG
        8   permission:android.permission.READ_PHONE_STATE
        9   permission:android.permission.CALL_PHONE
        10   permission:android.permission.WRITE_CALL_LOG
        11   permission:android.permission.USE_SIP
        12   permission:android.permission.PROCESS_OUTGOING_CALLS
        13   permission:com.android.voicemail.permission.ADD_VOICEMAIL
        14
        15 group:android.permission-group.CALENDAR
        16   permission:android.permission.READ_CALENDAR
        17   permission:android.permission.WRITE_CALENDAR
        18
        19 group:android.permission-group.CAMERA
        20   permission:android.permission.CAMERA
        21
        22 group:android.permission-group.SENSORS
        23   permission:android.permission.BODY_SENSORS
        24
        25 group:android.permission-group.LOCATION
        26   permission:android.permission.ACCESS_FINE_LOCATION
        27   permission:android.permission.ACCESS_COARSE_LOCATION
        28
        29 group:android.permission-group.STORAGE
        30   permission:android.permission.READ_EXTERNAL_STORAGE
        31   permission:android.permission.WRITE_EXTERNAL_STORAGE
        32
        33 group:android.permission-group.MICROPHONE
        34   permission:android.permission.group:android.permission-group.CONTACTS
        2   permission:android.permission.WRITE_CONTACTS
        3   permission:android.permission.GET_ACCOUNTS
        4   permission:android.permission.READ_CONTACTS
        5
        6 group:android.permission-group.PHONE
        7   permission:android.permission.READ_CALL_LOG
        8   permission:android.permission.READ_PHONE_STATE
        9   permission:android.permission.CALL_PHONE
        10   permission:android.permission.WRITE_CALL_LOG
        11   permission:android.permission.USE_SIP
        12   permission:android.permission.PROCESS_OUTGOING_CALLS
        13   permission:com.android.voicemail.permission.ADD_VOICEMAIL
        14
        15 group:android.permission-group.CALENDAR
        16   permission:android.permission.READ_CALENDAR
        17   permission:android.permission.WRITE_CALENDAR
        18
        21
        22 group:android.permission-group.SENSORS
        23   permission:android.permission.BODY_SENSORS
        24
        25 group:android.permission-group.LOCATION
        26   permission:android.permission.ACCESS_FINE_LOCATION
        27   permission:android.permission.ACCESS_COARSE_LOCATION
        28
        29 group:android.permission-group.STORAGE
        30   permission:android.permission.READ_EXTERNAL_STORAGE
        31   permission:android.permission.WRITE_EXTERNAL_STORAGE
        32
        35
        36 group:android.permission-group.SMS
        37   permission:android.permission.READ_SMS
        38   permission:android.permission.RECEIVE_WAP_PUSH
        39   permission:android.permission.RECEIVE_MMS
        40   permission:android.permission.RECEIVE_SMS
        41   permission:android.permission.SEND_SMS
        42   permission:android.permission.READ_CELL_BROADCASTS
        35
        36 group:android.permission-group.SMS
        37   permission:android.permission.READ_SMS
        38   permission:android.permission.RECEIVE_WAP_PUSH
        39   permission:android.permission.RECEIVE_MMS
        40   permission:android.permission.RECEIVE_SMS
        41   permission:android.permission.SEND_SMS
        42   permission:android.permission.READ_CELL_BROADCASTS
         */
        deniedList.forEach {
            if (it.endsWith("RECORD_AUDIO")) {
                result.append(" ${index++}、麦克风权限 \n")
            } else if (it.endsWith("CAMERA")) {
                result.append(" ${index++}、相机权限 \n")
            } else if (it.endsWith("STORAGE") && !result.contains("存储权限")) {
                result.append(" ${index++}、存储权限 \n")
            } else if (it.endsWith("LOCATION") && !result.contains("位置权限")) {
                result.append(" ${index++}、位置权限（GPS开关） \n")
            }else if(it== Manifest.permission.ACCESS_NETWORK_STATE){
                result.append(" ${index++}、网络状态 \n")
            }else if(it== Manifest.permission.ACCESS_WIFI_STATE){
                result.append(" ${index++}、Wifi状态 \n")
            }else if(it== Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS){
                result.append(" ${index++}、访问地点 \n")
            }else if(it== Manifest.permission.CHANGE_WIFI_STATE){
                result.append(" ${index++}、更改无线状态 \n")
            }else if(it== Manifest.permission.READ_PHONE_STATE){
                result.append(" ${index++}、手机读取状态 \n")
            }else if(it== Manifest.permission.ACCESS_FINE_LOCATION){
                result.append(" ${index++}、发现位置 \n")
            }else if(it== Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS){
                result.append(" ${index++}、访问额外地点 \n")
            }
        }
        return result.toString()
    }

}

interface PermissionXUtilsCallback {

    fun permissionCallback()

}